package com.scpii.universal.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import javax.servlet.http.HttpServletResponse;

/*
 * 数据流处理类
 * @yuejunyan
 */
public final class StreamUtil {

	private static final int BUFFER_SIZE = 2048;

	public final static byte[] convertToBytes(InputStream in) throws Exception {
		ByteArrayOutputStream reqOut = new ByteArrayOutputStream();
		byte[] buffer = new byte[BUFFER_SIZE];
		int count = 0;
		while ((count = in.read(buffer, 0, BUFFER_SIZE)) != -1) {
			reqOut.write(buffer, 0, count);
		}
		byte[] req = reqOut.toByteArray();
		reqOut.close();
		return req;
	}

	public final static OutputStream writeBytesToStream(OutputStream out,
			byte[] in) throws Exception {
		int offer = 0;
		while (offer + BUFFER_SIZE <= in.length) {
			out.write(in, offer, BUFFER_SIZE);
			offer += BUFFER_SIZE;
		}
		int length = in.length - offer;
		out.write(in, offer, length);
		out.flush();
		out.close();
		return out;
	}

	private static void transData(byte[] dataBytes, OutputStream outputStream) {
		// 传输数据
		try {
			StreamUtil.writeBytesToStream(outputStream, dataBytes);
			outputStream.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 传输数据方法
	 * 
	 * @param dataBytes
	 * @param response
	 */
	public final static void transData(byte[] dataBytes,
			HttpServletResponse response) {
		if (dataBytes != null && dataBytes.length > 0) {
			try {
				transData(dataBytes, response.getOutputStream());
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/***
	 * 压缩GZip
	 * 
	 * @param data
	 * @return
	 */
	public static byte[] gZip(byte[] data) {
		byte[] b = null;
		try {
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			GZIPOutputStream gzip = new GZIPOutputStream(bos);
			gzip.write(data);
			gzip.finish();
			gzip.close();
			b = bos.toByteArray();
			bos.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return b;
	}

	/***
	 * 解压GZip
	 * 
	 * @param data
	 * @return
	 */
	public static byte[] unGZip(byte[] data) {
		byte[] b = null;
		try {
			ByteArrayInputStream bis = new ByteArrayInputStream(data);
			GZIPInputStream gzip = new GZIPInputStream(bis);
			byte[] buf = new byte[BUFFER_SIZE];
			int num = -1;
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			while ((num = gzip.read(buf, 0, buf.length)) != -1) {
				baos.write(buf, 0, num);
			}
			b = baos.toByteArray();
			baos.flush();
			baos.close();
			gzip.close();
			bis.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return b;
	}

	/***
	 * 压缩Zip
	 * 
	 * @param data
	 * @return
	 */
	public static byte[] zip(byte[] data) {
		byte[] b = null;
		try {
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			ZipOutputStream zip = new ZipOutputStream(bos);
			ZipEntry entry = new ZipEntry("zip");
			entry.setSize(data.length);
			zip.putNextEntry(entry);
			zip.write(data);
			zip.closeEntry();
			zip.close();
			b = bos.toByteArray();
			bos.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return b;
	}

	/***
	 * 解压Zip
	 * 
	 * @param data
	 * @return
	 */
	public static byte[] unZip(byte[] data) {
		byte[] b = null;
		try {
			ByteArrayInputStream bis = new ByteArrayInputStream(data);
			ZipInputStream zip = new ZipInputStream(bis);
			while (zip.getNextEntry() != null) {
				byte[] buf = new byte[1024];
				int num = -1;
				ByteArrayOutputStream baos = new ByteArrayOutputStream();
				while ((num = zip.read(buf, 0, buf.length)) != -1) {
					baos.write(buf, 0, num);
				}
				b = baos.toByteArray();
				baos.flush();
				baos.close();
			}
			zip.close();
			bis.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return b;
	}
}
